//
// Created by 刘洋 on 2024/3/2.
//

#ifndef FOC7_INLINECURRENT_H
#define FOC7_INLINECURRENT_H
#include "adc.h"
#include "time_utils.h"
#include "foc_utils.h"
#include "AS5600.h"
#include "lowpass_filter.h"
#define _ADC_VOLTAGE 3.3f            //ADC 电压
#define _ADC_RESOLUTION 4096.0f      //ADC 分辨率
// ADC 计数到电压转换比率求解
#define _ADC_CONV ( (_ADC_VOLTAGE) / (_ADC_RESOLUTION) )

extern uint16_t raw_adc[2];
extern int PP;
extern float zero_electric_angle;
extern int DIR;
extern LowPassFilter CurrentFilter;
typedef struct
{
    float current_a,current_b,current_c;
    int pinA;
    int pinB;
    int pinC;
    float offset_ia;
    float offset_ib;
    float offset_ic;
    float _shunt_resistor;
    float amp_gain;

    float volts_to_amps_ratio;

    float gain_a;
    float gain_b;
    float gain_c;
    ADC_HandleTypeDef *hadc;
}CurrSense;
float readADCVoltageInline(CurrSense* sensor,uint8_t ChennalNumber);
void CurrSenseCalibrateOffsets(CurrSense* sensor);
void CurrSenseInit(CurrSense* sensor,ADC_HandleTypeDef *hadc,float R_value,float sensor_gain,int pinAIsDef,int pinBIsDef,int pinCIsDef,int sensorDir_A,int sensorDir_B,int sensorDir_C);
void getPhaseCurrents(CurrSense* sensor);
float getIqId(CurrSense sensor);
float cal_Iq_Id(float current_a,float current_b,float angle_el);
#endif //FOC7_INLINECURRENT_H
